package com.zuipin.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.entity.Member;
import com.zuipin.entity.MemberAccount;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.exception.CustomException;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IMemberAccountService;
import com.zuipin.service.IMemberRecordService;
import com.zuipin.service.IMemberService;
import com.zuipin.service.IThirdAccountService;
import com.zuipin.util.ConstantsUtil;
import com.zuipin.util.RegexUtils;
import com.zuipin.vo.MemberInfoVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName: MemberAccountController
 * @author wuyicheng
 * @time 2017年3月27日下午4:11:16
 */
@RestController
@Api(value = "会员账户信息", description = "会员账户信息-wuyicheng")
@RequestMapping(value = "/member/memberAccount")
public class MemberAccountController {
	@Resource
	private IMemberAccountService	memberAccountService;
	@Resource
	private IMemberService			memberService;
	@Resource
	private IMemberRecordService	memberRecordService;
	@Resource
	private IThirdAccountService	thirdAccountService;
	
	@RequestMapping(value = "/searchIsExist", method = RequestMethod.GET)
	@ApiOperation(value = "判断账号是否存在-wuyicheng", notes = "判断账号是否存在-wuyicheng")
	@Valids({ @Valid(name = "memberAccount", regex = RegexUtils.MOBILE_FUZZY), @Valid(name = "sysId", minValue = 0) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberAccount", value = "会员账号", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	public Result<Map<String, Object>> searchIsExist(@RequestParam String memberAccount, @RequestParam Long sysId) {
		Result<Map<String, Object>> result = new Result<>();
		Map<String, Object> map = new HashMap<>();
		boolean flag = memberAccountService.doIsExist(memberAccount, sysId);
		if (flag) {
			map.put(ConstantsUtil.RETURN_RESULT, true);
		} else {
			map.put(ConstantsUtil.RETURN_RESULT, false);
		}
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setData(map);
		return result;
	}
	
	@RequestMapping(value = "/findPwd", method = RequestMethod.POST)
	@ApiOperation(value = "找回密码-wuyicheng", notes = "找回密码-wuyicheng")
	@Valids({ @Valid(name = "memberAccount", regex = RegexUtils.MOBILE_FUZZY), @Valid(name = "password", minLength = 6, maxLength = 16), @Valid(name = "sysId", minValue = 0) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberAccount", value = "用户账号", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "password", value = "用户密码", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	public Result<Map<String, Object>> findPwd(@RequestParam String memberAccount, @RequestParam Long sysId, @RequestParam String password) {
		Result<Map<String, Object>> result = new Result<>();
		result = memberAccountService.findPwd(memberAccount, password, sysId, result);
		return result;
	}
	
	@RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
	@ApiOperation(value = "修改密码-wuyicheng", notes = "修改密码-wuyicheng")
	@Valids({ @Valid(name = "memberId", required = true, regex = RegexUtils.MOBILE_FUZZY), //
			@Valid(name = "password", minLength = 6, maxLength = 16), //
			@Valid(name = "newPwd", minLength = 6, maxLength = 16), @Valid(name = "sysId", minValue = 0) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "password", value = "旧密码", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "newPwd", value = "新密码", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	public Result<Map<String, Object>> updatePwd(@RequestParam Long memberId, @RequestParam Long sysId, @RequestParam String password, @RequestParam String newPwd) {
		Result<Map<String, Object>> result = new Result<>();
		result = memberAccountService.updatePwd(memberId, sysId, password, newPwd, result);
		return result;
	}
	
	@RequestMapping(value = "/updatePhone", method = RequestMethod.POST)
	@ApiOperation(value = "修改绑定的手机-wuyicheng", notes = "修改绑定的手机-wuyicheng" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常" + "</br>3006-会员不存在"
			+ "</br>3007-会员账号不存在" + "</br>3008-手机好已经绑定另一个账号" + "</br>3009-第三方账号不存在" + "</br>4031-两个账号分别绑定不同手机号，无法修改" + "</br>4032-该手机号已经存在一个相同平台的第三方账号")
	@Valids({ @Valid(name = "phone", regex = RegexUtils.MOBILE_FUZZY), @Valid(name = "isThirderAccount", regex = "true|false") })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "用户ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "phone", value = "绑定手机名", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "isThirderAccount", value = "当前是否是第三方帐号(true|false) 选true，平台类型ID是必填的", required = true, dataType = "boolean", paramType = "query"),
			@ApiImplicitParam(name = "platformId", value = "平台类型ID:360安全用户=46,腾讯QQ用户=47,支付宝用户=48,微信用户=49,迅雷用户=50", required = false, dataType = "long", paramType = "query"), })
	public Result<MemberInfoVo> updatePhone(@RequestParam Long memberId, @RequestParam String phone, @RequestParam Long sysId, @RequestParam boolean isThirderAccount,
			@RequestParam(required = false) Long platformId) throws CustomException {
		Result<MemberInfoVo> result = new Result<>();
		result = thirdAccountService.toUpdatePhone(memberId, sysId, phone, isThirderAccount, platformId, true);
		
		return result;
		
	}
	
	@RequestMapping(value = "/findByOneMember", method = RequestMethod.GET)
	@ApiOperation(value = "根据memberId和系统ID查询会员账户信息-wuyicheng", notes = "根据memberId和系统ID查询会员账户信息-wuyicheng")
	@Valids({ @Valid(name = "memberId", minValue = 0), @Valid(name = "sysId", minValue = 0) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	public Result<MemberInfoVo> findByOneMember(@RequestParam Long memberId, @RequestParam Long sysId) {
		Result<MemberInfoVo> result = new Result<>();
		Member member = memberService.findById(memberId);
		if (member != null) {
			MemberAccount memberAccount = new MemberAccount();
			memberAccount.setMemberId(memberId);
			memberAccount.setSysId(sysId);
			memberAccount = memberAccountService.findByMemberAccount(memberAccount);
			if (memberAccount != null) {
				MemberInfoVo memberInfoVo = new MemberInfoVo();
				memberInfoVo.setMember(member);
				memberInfoVo.setMemberAccount(memberAccount);
				result.setErrorCode(ErrorCode.SUCCESS);
				result.setData(memberInfoVo);
			} else {
				result.setErrorCode(ErrorCode.NOT_FIND);
				result.setMessage("memberAccount is not exist");
				result.setData(null);
			}
		} else {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("member is not exist");
			result.setData(null);
		}
		return result;
	}
	
	// @RequestMapping(value = "/updateMemberByAccount", method = RequestMethod.POST)
	// @ApiOperation(value = "根据memberId和系统ID修改会员信息-wuyicheng", notes = "根据memberId和系统ID修改会员信息-wuyicheng")
	// @Valids({ @Valid(name = "memberId", minValue = 0),
	// @Valid(name = "sysId", minValue = 0) })
	// @ApiImplicitParams({
	// @ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "long", paramType = "query"),
	// @ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	// public Result<Map<String, Object>> updateMemberByAccount(
	// @Inject @ApiIgnore MemberAccount memberAccount) {
	// Result<Map<String, Object>> result = new Result<>();
	// Map<String, Object> jsonData = new HashMap<>();
	// if (memberAccount != null) {
	// //MemberAccount userAccount=memberAccountService.findByMemberAccount(memberAccount);
	// //BeanUtils.copyProperties(memberAccount, userAccount);
	// memberAccountService.update(memberAccount);
	// jsonData.put(ConstantsUtil.RETURN_RESULT, true);
	// result.setErrorCode(ErrorCode.SUCCESS);
	// } else {
	// jsonData.put(ConstantsUtil.RETURN_RESULT, false);
	// result.setErrorCode(ErrorCode.NULL_POINT);
	// }
	// result.setErrorCode(ErrorCode.SUCCESS);
	// result.setData(jsonData);
	// return result;
	// }
	@ApiImplicitParams({ @ApiImplicitParam(name = "unionId", value = "unionId", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "platformId", value = "platformId", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	@RequestMapping(value = "/findMemberByUnionId", method = RequestMethod.GET)
	@ApiOperation(value = "根据unionId获取会员信息", notes = "根据unionId获取会员信息")
	public Result<MemberAccount> findMemberByUnionId(@RequestParam String unionId, @RequestParam Long sysId, @RequestParam Long platformId) {
		Result<MemberAccount> result = new Result<MemberAccount>();
		result = memberAccountService.findMemberByUnionId(unionId, sysId, platformId, result);
		return result;
	}
	
	@Valids({ @Valid(name = "memberId", minValue = 0), @Valid(name = "sysId", minValue = 0), @Valid(name = "password", minValue = 1) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "password", value = "会员密码", required = true, dataType = "String", paramType = "query"), })
	@RequestMapping(value = "/selectPwdIsEquals", method = RequestMethod.POST)
	@ApiOperation(value = "判断密码是否正确", notes = "判断密码是否正确")
	public Result<Map<String, Object>> selectPwdIsEquals(@RequestParam Long memberId, @RequestParam Long sysId, @RequestParam String password) {
		Result<Map<String, Object>> result = new Result<>();
		MemberAccount memberAccount = memberAccountService.findByMemberIdAndSysId(memberId, sysId);
		if (memberAccount != null) {
			boolean pwdIsEqualsFlag = memberAccountService.pwdIsEquals(password, memberAccount.getPassword());
			Map<String, Object> map = new HashMap<>();
			map.put(ConstantsUtil.RETURN_RESULT, pwdIsEqualsFlag);
			result.setData(map);
			result.setErrorCode(ErrorCode.SUCCESS);
		} else {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("memberAccount is not exist.");
		}
		return result;
	}
	
}